package com.inspectortime.webapp.util;

import java.beans.PropertyEditorSupport;

import org.springframework.util.StringUtils;

import com.inspectortime.domain.type.State;

/**
 * Simple property editor for the State enum. 
 * By default, Spring will throw a typeMismatch error if an empty string is bound to a State attribute,
 * because the empty string is not a constant in an Enum.  This will convert the empty string to a null,
 * so that you can choose to 'null out' a state attribute
 * 
 * @author bones
 *
 */
public class CustomStateEditor extends  PropertyEditorSupport {
	
	public String getAsText() {
		State state = (State) getValue();
		return (state != null ? state.name() : "");
	}
	
	public void setAsText(String text) throws IllegalArgumentException {
		if (!StringUtils.hasText(text)) {
			setValue(null);
		} else {
			setValue(State.valueOf(text));
		}
	}

}
